/*
 * Copyright (c) 2006-2025, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-09-22     RT-Thread    first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <spi_flash.h>
#include <spi_flash_sfud.h>
#include <drv_spi.h>

// 添加文件系统相关头文件
#include <dfs_fs.h>
#include <dfs_elm.h>

#include "mqtt_client.h"  // 添加MQTT客户端头文件

#define SPI_FLASH_DEVICE_NAME "spi10"


int main(void)
{
    rt_kprintf("=== AIoT Intelligence System Started ===\n");
    rt_kprintf("Board: STM32F407VET6\n");
    rt_kprintf("Features: W25Q128 Flash, LAN8720A Ethernet, MQTT Client\n");

    // 初始化LED状态
    led_init();

    rt_kprintf("System initialized. MQTT client will start automatically.\n");
    rt_kprintf("Use 'cmd_led' command to manually control LEDs.\n");

    return 0;
}



//falsh初始化函数
static int rt_hw_spi_flash_init(void)
{
    /* 禁用 JTAG 以释放 PA15、PB3、PB4 引脚 - STM32F4 系列使用 SYSCFG */
    __HAL_RCC_SYSCFG_CLK_ENABLE();

    // 直接操作 SYSCFG_PMC 寄存器来禁用 JTAG 但保留 SWD
    SYSCFG->PMC &= ~(0x7 << 24);
    SYSCFG->PMC |= (0x2 << 24);

    __HAL_RCC_GPIOA_CLK_ENABLE(); // 启用对应 GPIO 时钟
    __HAL_RCC_GPIOB_CLK_ENABLE();

    // 配置PA15为推挽输出(CS引脚)
    rt_pin_mode(GET_PIN(A,15), PIN_MODE_OUTPUT);
    rt_pin_write(GET_PIN(A,15), PIN_HIGH);

    //附加SPI设备
    if (rt_hw_spi_device_attach("spi1", "spi10", GPIOA, GPIO_PIN_15) != RT_EOK) {
        rt_kprintf("Failed to attach SPI device!\n");
        return -RT_ERROR;
    }

    // 初始化SPI Flash
    rt_kprintf("Starting SPI flash probe...\n");

    // 添加延迟
    rt_thread_mdelay(100);

    // 在 rt_hw_spi_flash_init 中调用验证函数
    if (rt_sfud_flash_probe("W25Q128", "spi10") == RT_NULL) {
        rt_kprintf("SPI flash probe failed!\n");
        return -RT_ERROR;
    } else {
        rt_kprintf("SPI flash probe success!\n");
    }

    rt_device_t dev = rt_device_find("W25Q128");
    if (dev) {
        struct rt_device_blk_geometry geometry;
        rt_device_control(dev, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
        rt_kprintf("sector size: %d, sector count: %d\n", geometry.bytes_per_sector, geometry.sector_count);
    }

    // 添加挂载代码
    if (dfs_mount("W25Q128", "/", "elm", 0, 0) == 0) {
        rt_kprintf("Flash file system mounted to /\n");
    } else {
        rt_kprintf("Flash file system mount failed! Trying to reformat...\n");

    }
}
INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init);


//flash挂载指令
static void format_flash(int argc, char **argv) {
    rt_kprintf("Formatting flash...\n");
    int ret = dfs_mkfs("elm", "W25Q128");
    if (ret == RT_EOK) {
        rt_kprintf("Format successful. Please reboot.\n");
    } else {
        rt_kprintf("Format failed: %d\n", ret);
    }
}
MSH_CMD_EXPORT(format_flash, Format the flash chip);

// 系统状态命令
static void system_status(int argc, char **argv) {
    rt_kprintf("=== System Status ===\n");
    rt_kprintf("Board: STM32F407VET6\n");
    rt_kprintf("LED Red: PE7, LED Blue: PE8\n");
    rt_kprintf("Flash: W25Q128\n");
    rt_kprintf("Network: LAN8720A\n");
    rt_kprintf("MQTT: Auto-start enabled\n");
}
MSH_CMD_EXPORT(system_status, Show system status);
